Typical Usage:
Required bean name is "CAN".
There are the following typical usage examples:
(1) Acceptance code filtering
It is necessary to set acceptance filter properly, before a message can be successively received.
Acceptance filtering may be derivative dependent and property names or method parameters may
differ from the example below (see Properties and Methods page). Each message has an identifier (ID)
when sent over CAN bus. The ID of sent message is specified in a parameter of SendFrame method (transmitter node).
The acceptance filter on a receiver node must be set to the same ID to pass the received message to receive buffer.
On the receiver node acceptance filter is set by following:
- Acceptance Code property - The ID of received message is set in the initialization code of the bean
- SetAcceptanceCode method - The ID of received message may be changed at runtime.
The messages with different ID can be received using an acceptance mask. The acceptance mask specifies which bits
in the acceptance code are ignored.
- Acceptance Mask property - The mask for ID of received message is set in the initialization code of the bean
- SetAcceptanceMask method - The mask for ID of received message may be changed at runtime.
MAIN.C
void main(void)
{
//Sets the code of accepted message.
CAN_SetAcceptanceCode(0x15263740);
//Set the acceptance mask to 3, i.e. messages with IDs
//0x15263740,0x15263741,0x15263742 and 0x15263743 are accepted
CAN_SetAcceptanceMask(0x00000003);
//...
//All messages are accepted
CAN_SetAcceptanceMask(0xFFFFFFFF);
//...
}
(2) Communication without using interrupts (polling mode).
Set Interrupt service/event property to Disabled.
SendFrame method sends a frame with data to CAN bus.
Method
MAIN.C
byte buff[]={1,2,3,4,5,6,7,8};
void main(void)
{
for(;;) {
//Send data frame via buffer 0 with ID=0x512.
//The buffer consists of 8 bytes.
CAN_SendFrame(0, 0x512, DATA_FRAME, 8, buff);
}
}
(3) Standard and Extended identifier sending
The message ID passed to SendFrame method is either standard or extended type.
The extended ID is marked by the most significant bit set
(CAN_EXTENDED_FRAME_ID symbol is defined, see example below).
MAIN.C
byte buff[]={1,2,3,4,5,6,7,8};
dword messageID = 0x512;
void main(void)
{
//Send data frame via buffer 0 with extended ID=0x512.
CAN_SendFrame(0,(messageID|CAN_EXTENDED_FRAME_ID),DATA_FRAME,8,buff);
//Send data frame via buffer 0 with standart ID=0x512.
CAN_SendFrame(0,messageID, DATA_FRAME, 8, buff);
}
(4)
Communication with interrupts and using communication events.
OnFullRxBuffer event is called when a frame is successfully received.
SendFrame method sends a frame with data to CAN bus.
ReadFrame method reads a received frame from buffer.
EVENTS.C
void CAN1_OnFullRxBuffer(void)
{
dword ID;
byte tupe, len;
byte buff[];
word bufferMask;
//Test what message buffer have received data.
//Only for HW CAN implementations with more than one receive buffer
if (CAN1_GetStateRX() & CAN_MB0_MASK) {
//Read received data in message buffer 0 configured as receive
//Note: the first parameter "BufferNum" in method ReadFrame
//is valid for FlexCAN modules only
//if they have more than one receive buffer
CAN_ReadFrame(0, &ID, &type, &len, buff);
//Send data back via message buffer 1 configured as transmit
CAN_SendFrame(1, ID, type, len, buff);
}
}
For more about typical usage of the bean code please refer to the page Bean Code Typical Usage.
|